require('./../models/disciplina.model.js');
var DBAccess = require('./db');

function DisciplinaDAO() {
    var db = DBAccess.getInstance();
    var model = db.model('Disciplina');


    this.add = function(disc, callback) {
        db.connect();
        var args = arguments;
        disc.save(function(err) {
            if(err) throw err;
            else if (args.length === 2 && typeof args[1] == 'function') 
                callback();
        });
    }

    this.remove = function(disc, callback) {
        db.connect();
        var args = arguments;
        model.remove({nome: disc.nome}, function(err, doc) {
            if (err) throw err;
            else if (args.length === 2 && typeof args[1] == 'function')
                callback();
        });
    }

    this.get = function(nome, callback) {
        db.connect();
        model.findOne({nome: nome}, function(err, doc) {
            if(!doc) throw new Error('Not Found!');
            callback(err, doc);
        });
    }

    this.getById = function(id, callback) {
        db.connect();
        model.findById(id, callback);
    }

    //TODO
    this.update = function(id, updated) {

    }

    this.getAll = function(callback) {
        db.connect();
        if(typeof callback != 'function')
            throw new Error('Give me a callback function!');
        model.find({}, function(err, all) {
            callback(err, all);
        });
    }

    this.getModel = function() {
        return new model();
    }
}

module.exports = DisciplinaDAO;
